package com.industrial.premu.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.SysArea;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.mapper.PreMuEnvironmentalIndicatorAnalysisMapper;
import com.industrial.premu.mapper.SysAreaMapper;
import com.industrial.premu.service.IPreMuEnvironmentalIndicatorAnalysisService;
import com.industrial.premu.vo.PreMuChartsVo;
import io.swagger.models.auth.In;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class PreMuEnvironmentalIndicatorAnalysisServiceImpl implements IPreMuEnvironmentalIndicatorAnalysisService {

    @Autowired
    private PreMuEnvironmentalIndicatorAnalysisMapper preMuEnvironmentalIndicatorAnalysisMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * <p>
     *     查询近5年的数据
     * </p>
     * @param preMuChartsDto
     */
    @Override
    public AjaxResult queryDataFromThePastFiveYears(PreMuChartsDto preMuChartsDto) {
        List<PreMuBenefitEvaluation> xianMuBenefitEvaluations = preMuEnvironmentalIndicatorAnalysisMapper.queryDataFromThePastFiveYears(preMuChartsDto);

        boolean shengFlag = false;
        List<PreMuBenefitEvaluation> shengPreMuBenefitEvaluation = new ArrayList<>();

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            shengFlag = true;
            preMuChartsDto.setCounty(null);
            preMuChartsDto.setProvince("370000");
            shengPreMuBenefitEvaluation = preMuEnvironmentalIndicatorAnalysisMapper.queryDataFromThePastFiveYears(preMuChartsDto);
        }

        //年汇总到一块
        Set<Integer> year = new HashSet<>();

        //建立年到值的映射
        Map<String,Object> xianMapping = new HashMap<>(16);
        Map<String,Object> shengMapping = new HashMap<>(16);

        //县的年度放到了Set中
        for(PreMuBenefitEvaluation item : xianMuBenefitEvaluations){
            year.add(Integer.valueOf(item.getYears()));
            xianMapping.put(item.getYears(),item.getAmmonia());
        }

        if(shengFlag){
            for(PreMuBenefitEvaluation item : shengPreMuBenefitEvaluation){
                year.add(Integer.valueOf(item.getYears()));
                shengMapping.put(item.getYears(),item.getAmmonia());
            }
        }

        //对年度进行排序
        if(!year.isEmpty()){
            year = year.stream().sorted(Integer::compareTo).collect(Collectors.toCollection(LinkedHashSet::new));
        }

        List<Object> xianResult = new ArrayList<>();
        List<Object> shengResult = new ArrayList<>();

        for(Integer y : year){

            String yearStr = String.valueOf(y);
            Object o = xianMapping.get(yearStr);
            xianResult.add(o);
            if(shengFlag){
                Object o1 = shengMapping.get(yearStr);
                shengResult.add(o1);
            }
        }

        Map<String,Object> result = new HashMap<>();
        result.put("x",year);
        result.put("xian",xianResult);
        result.put("sheng",shengResult);
        return AjaxResult.success(result);
    }

    /**
     * 查询区域排名情况
     *
     * @param preMuChartsDto
     * @return
     */
    @Override
    public List<PreMuBenefitEvaluation> queryIndexAreaOrderTable(PreMuChartsDto preMuChartsDto) {

        List<PreMuBenefitEvaluation> result = preMuEnvironmentalIndicatorAnalysisMapper.queryIndexAreaOrder(preMuChartsDto);

        return result;
    }

    @Override
    public AjaxResult queryIndexAreaOrderEcharts(PreMuChartsDto preMuChartsDto) {
        List<PreMuBenefitEvaluation> result = preMuEnvironmentalIndicatorAnalysisMapper.queryIndexAreaOrder(preMuChartsDto);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     * 查询行业排名图表
     * </p>
     *
     * @param preMuChartsDto
     * @return
     */
    @Override
    public AjaxResult queryIndexHyOrderEcharts(PreMuChartsDto preMuChartsDto) {

        List<PreMuBenefitEvaluation> result = preMuEnvironmentalIndicatorAnalysisMapper.queryIndexHyOrderEcharts(preMuChartsDto);

        return AjaxResult.success(result);
    }

    /**
     * <p>
     * 查询行业排名表格
     * </p>
     *
     * @param preMuChartsDto
     * @return
     */
    @Override
    public List<PreMuBenefitEvaluation> queryIndexHyOrderTable(PreMuChartsDto preMuChartsDto) {
        List<PreMuBenefitEvaluation> result = preMuEnvironmentalIndicatorAnalysisMapper.queryIndexHyOrderEcharts(preMuChartsDto);
        return result;
    }

    @Override
    public List<PreMuChartsVo> groupEconomyByYears(PreMuChartsDto preChartsDto) {
        return preMuEnvironmentalIndicatorAnalysisMapper.groupEconomyByYears(preChartsDto);
    }

    @Override
    public List<PreMuChartsVo> groupEconomyByCityAndCounty(PreMuChartsDto preChartsDto) {
        return preMuEnvironmentalIndicatorAnalysisMapper.groupEconomyByCityAndCounty(preChartsDto);
    }

    @Override
    public List<PreMuChartsVo> groupEconomyByProvince(PreMuChartsDto preChartsDto) {
        return preMuEnvironmentalIndicatorAnalysisMapper.groupEconomyByProvince(preChartsDto);
    }
}
